package 剑指offer;

/**
 * @Description:
 * @Author: Administrator
 * @Date: 2020-05-05
 */
public class 平衡二叉树 {
    public static boolean IsBalanced_Solution(TreeNode root) {

        if (root == null) {
            return false;
        }
        return Math.abs(deepTree(root.left,  1)
                -deepTree(root.right, 1))>1?false:true;
    }

    private static int deepTree(TreeNode root, int deep) {
        if (root == null) {
            return deep;
        }
        //  return max - min > 1 ? false : true;
        return Math.max(deepTree(root.left ,deep + 1),
                deepTree(root.right, deep + 1));

    }
    public static void main(String[] args) {
        TreeNode root = new TreeNode(8);
        root.left = new TreeNode(6);
        root.right = new TreeNode(10);
        root.left.left = new TreeNode(5);
        root.left.right = new TreeNode(7);
        root.right.left = new TreeNode(9);
        root.right.right = new TreeNode(11);
        System.out.println(IsBalanced_Solution(root));
    }

}
